<?php

namespace App\Admin\Controllers\Order;

use App\Model\Commodity;
use App\Model\Commodity_color;
use App\Model\Commodity_info;
use App\Model\Commodity_size;
use App\Model\Order;
use App\Model\Parent_order;
use App\Model\User_address;
use App\Model\User;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class OrderController extends Controller
{
    use ModelForm;

    /**
     * @var Order
     */
    private $order;
    /**
     * @var Parent_order
     */
    private $parent_order;
    /**
     * @var User_address
     */
    private $user_address;
    /**
     * @var \Illuminate\Config\Repository|mixed
     */
    private $state_back;

    /**
     * OrderController constructor.
     * @param Order $order
     * @param Parent_order $parent_order
     * @param Request $request
     * @param User_address $user_address
     */
    function __construct(Order $order, Parent_order $parent_order, User_address $user_address)

    {
        $this->order = $order;
        $this->parent_order = $parent_order;
        $this->user_address = $user_address;
        $this->state_back = config("bar.parent_order.admin_state");
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('订单');
            $content->description('订单管理');

            $data = $this->getOrder();
            $content->body(view("Admin.Order.index", $data));
        });
    }

    private function getOrder()
    {
        $data = array();

        $parent_order = $this->parent_order->all();

        $parent_order = $parent_order->toArray();

        if (!empty($parent_order)) {
            foreach ($parent_order as $key => $value) {
                $data["parent_order"][$key] = $value;
                $data["parent_order"][$key]["state_name"] = $this->state_back[$value["state"]];

                $this->user_address = new User_address();
                $this->user_address = $this->user_address->find($value["user_address_id"]);
                $data["parent_order"][$key]["user_address"] = $this->user_address->toArray();

                $this->user = new User;
                $this->user = $this->user->find($value["user_id"]);
                $data["parent_order"][$key]["user"] = $this->user->toArray();

                $this->order = new Order();
                $order = $this->order->getOrderByParent($value["parent_order_id"]);
                if (empty($order))
                    $data["parent_order"][$key]["data"][] = $order;
                else {
                    foreach ($order as $k => $v) {
                        $CommodityModel = new Commodity();
                        $colorModel = new Commodity_color();
                        $sizeModel = new Commodity_size();
                        $infoModel = new Commodity_info();
                        $commodityRes = $CommodityModel->get_commodity_by_commidity_id($v["commodity_id"]);
                        if (empty($commodityRes) || count($commodityRes) == 0) {
                            unset($order[$key]);
                            continue;
                        }
                        $v = array_merge($v, reset($commodityRes));
                        $v = array_merge($v, $colorModel->get_redis('commodity_color:' . $v["color_id"] . ':commodity:' . $v["commodity_id"]));
                        $v = array_merge($v, $sizeModel->get_redis('commodity_size:' . $v["size_id"] . ':commodity:' . $v["commodity_id"]));
                        $v = array_merge($v, $infoModel->get_redis("commodity_info:" . $v["info_id"] . ":commodity:" . $v["commodity_id"] . ":color:" . $v["color_id"] . ":size:" . $v["size_id"]));
                        unset($v["commodity_desc"]);
                        $data["parent_order"][$key]["data"][] = $v;
                    }
                }
            }
        } else {
            $data["parent_order"] = $parent_order;
        }
        $data["state"] = $this->state_back;
        return $data;
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('订单');
            $content->description('订单管理');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Parent_order::class, function (Form $form) {

            $form->display("parent_order_id", "parent_order_id");
            $form->display("user_id", "user_id");
            $form->display("trading_stream_id", "trading_stream_id");
            $form->display("all_price", "all_price");
            $form->display("created_at", "created_at");
            $form->display("updated_at", "updated_at");
            $form->display("state", "state");
            $form->display("user_address_id", "user_address_id");
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('订单');
            $content->description('订单管理');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Parent_order::class, function (Grid $grid) {

            $grid->parent_order_id("编号");
            $grid->user("创建者")->display(function ($user) {
                return "" . $user["nickname"] . "";

            });
            $grid->all_price("价格");
            $grid->created_at();

            $grid->order('商品')->display(function ($order) {
                $html = "";
                $colorModel = new Commodity_color();
                $sizeModel = new Commodity_size();
                $infoModel = new Commodity_info();
                foreach ($order as $k => $v) {
                    $v = array_merge($v, $colorModel->get_redis('commodity_color:' . $v["color_id"] . ':commodity:' . $v["commodity_id"]));
                    $v = array_merge($v, $sizeModel->get_redis('commodity_size:' . $v["size_id"] . ':commodity:' . $v["commodity_id"]));
                    $v = array_merge($v, $infoModel->get_redis("commodity_info:" . $v["info_id"] . ":commodity:" . $v["commodity_id"] . ":color:" . $v["color_id"] . ":size:" . $v["size_id"]));
                    $html .= "<img class='order_img' style='width:100px;height:100px;' src='" . $v["info_img_url"] . "' alt=''>";
                    $html .= "<span class=''>" . $v["commodity_color_name"] . ";" . $v["commodity_size_name"] . " x" . $v["number"] . "</span>";
                    $html .= "<br/>";
                }
                return $html;
            });
            $grid->address('收件人')->display(function ($address) {
                $address = $address;
                if (empty($address) && count($address) === 0)
                    return "无";
                return "<span class='' style='text-align: center'>" . $address["get_name"] . "<br/>" . $address["phone"] . "<br/>" . $address["address"] . "</span>";
            });
            $grid->state("状态")->display(function ($state, Grid $grid) {
                return "<span class='label label-warning state_play' data-state='$state'>" . config("bar.parent_order.admin_state")[$state] . "</span>";
            });
        });
    }
}
